package cn.alauncher.tcpdemo;

import cn.alauncher.tcpdemo.callback.TCPClientcallBack;
import cn.alauncher.tcpdemo.utils.ByteUtil;
import cn.alauncher.tcpdemo.utils.Constant;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.UnsupportedEncodingException;

@SpringBootApplication
public class TcpdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(TcpdemoApplication.class, args);
        doClient();
//        System.out.println("" + ByteUtil.convertHexToString("303032333030313820202020202020202020202030303100"));
//        System.out.println("" + ByteUtil.convertStringToHex("01"));
//        handlerMsg("02310061001 5151    010000025103                         04                         050006002070000080000091101111120000001300050014000000150000021601000170120018010801901081202020-03-27:08:01:35212020-03-19:04:53:49222230000000350");
    }

    public static TCPclient clent;

    public static void doClient() {
        clent = new TCPclient();
        clent.startTCPclient("192.168.5.212", 4545, new MTTCPClientCallBack());

        try {
            Thread.sleep(1000);
            clent.TCPClientSendHEX(Constant.COMMAND_START);
            clent.StartHreat(Constant.COMMAND_HREAT, 5);
            Thread.sleep(1000);
            clent.TCPClientSendHEX(cmdByHEX("02"));
            Thread.sleep(1000);
            clent.TCPClientSendHEX(Constant.COMMAND_LISTEN);


        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    static class MTTCPClientCallBack implements TCPClientcallBack {

        @Override
        public void Disconnected() {

        }

        @Override
        public void MssageArrived(byte[] readbuffer, int lenth) {
            try {
                String str = new String(readbuffer, 0, readbuffer.length, "utf-8");
                handlerMsg(str);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            System.out.println("MssageArrived HEX = " + ByteUtil.ByteArrToHex(readbuffer));
            // 解析马头返回字符；
        }

        @Override
        public void Hasconnect() {

        }

        @Override
        public void ConnectErro() {

        }
    }

    /*
     *** 不够10位的话前面补0，比如1是01;
     */
    static String cmdByHEX(String cmdID) {
        String hex = "303032333030313820202020202020202020202030" + ByteUtil.convertStringToHex(cmdID) + "00";
        return hex;
    }

    static void handlerMsg(String msg) {
        String mid = msg.substring(4, 8);
        System.out.println("mid = " + mid);
        switch (mid) {
            case "0061":
                if (clent != null) clent.TCPClientSendHEX(Constant.COMMAND_CONFIRM);
                TighteningResultData tighteningResultData = new TighteningResultData();
                tighteningResultData.setCellID(msg.substring(22, 26));
                tighteningResultData.setChannelID(msg.substring(28, 30));
                tighteningResultData.setAngleStatus(msg.substring(113,114));
                tighteningResultData.setTorqueStatus(msg.substring(110, 111));
                tighteningResultData.setTimeStamp(msg.substring(176, 195));
                tighteningResultData.setLastChandgeParameterSetSettings(msg.substring(197, 216));
                System.out.println(tighteningResultData);
                break;
        }
    }

    static class TighteningResultData {
        String cellID;
        String channelID;
        String torqueStatus;
        String angleStatus;
        String timeStamp;
        String lastChandgeParameterSetSettings;

        public String getTimeStamp() {
            return timeStamp;
        }

        public void setTimeStamp(String timeStamp) {
            this.timeStamp = timeStamp;
        }

        public String getCellID() {
            return cellID;
        }

        public void setCellID(String cellID) {
            this.cellID = cellID;
        }

        public String getChannelID() {
            return channelID;
        }

        public void setChannelID(String channelID) {
            this.channelID = channelID;
        }

        public String getTorqueStatus() {
            return torqueStatus;
        }

        public void setTorqueStatus(String torqueStatus) {
            this.torqueStatus = torqueStatus;
        }

        public String getAngleStatus() {
            return angleStatus;
        }

        public void setAngleStatus(String angleStatus) {
            this.angleStatus = angleStatus;
        }

        public String getLastChandgeParameterSetSettings() {
            return lastChandgeParameterSetSettings;
        }

        public void setLastChandgeParameterSetSettings(String lastChandgeParameterSetSettings) {
            this.lastChandgeParameterSetSettings = lastChandgeParameterSetSettings;
        }

        @Override
        public String toString() {
            return "TighteningResultData{" +
                    "cellID='" + cellID + '\'' +
                    ", channelID='" + channelID + '\'' +
                    ", torqueStatus='" + torqueStatus + '\'' +
                    ", angleStatus='" + angleStatus + '\'' +
                    ", timeStamp='" + timeStamp + '\'' +
                    ", lastChandgeParameterSetSettings='" + lastChandgeParameterSetSettings + '\'' +
                    '}';
        }
    }
}
